<?php
namespace app\index\controller;
use app\common\utils\ResultVOUtil;
use app\common\enums\ResultEnum;
use think\facade\Log;
use think\Request;
use app\common\model\Dispatch as dispatchModel;
use app\common\model\CarsPark;
use app\common\model\Cars;
use app\common\service\DispatchService;

class Dispatch extends Base
{
    protected $dispatchService;
    
    public function __construct(DispatchService $dispatchService)
    {
        parent::initialize();
        $this->dispatchService = $dispatchService;
    }
    
    public function index(Request $request,$page=1,$length=10){
        $data = $request->post();
        $where = [];
        
        if(!empty($data['search'])){
            $where[] = ['depart|receive|receive_name|receive_tel','like','%'.$data['search'].'%'];
        }
        
        try {
            $model = dispatchModel::where($where)
            ->paginate($length,false,[
                'type'     => 'bootstrap',
                'var_page' => 'page',
                'page'=>$page,
            ]);
            
        } catch (\Exception $e) {
            Log::record('车辆调拨:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
        return ResultVOUtil::success($model);
    }
    
    
    public function save(Request $request) {
        $data = $request->param();
        $model = new dispatchModel();
        try{
            
            $model->allowField(true)->save($data);
            if($model->id){
               return ResultVOUtil::success('添加成功');
               
            }else{ 
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
            
        }catch (\Exception $e){
            Log::record('车辆调拨:'.$e->getMessage(),'error');
            return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
        }
        
    }
    
    
    public function details($id=1){
        $data = [];
        $data['pay'] = dispatchModel::get($id);
        $data['cars'] = Cars::with('agency,brand,carsPark')->where('car_id','in',$data['pay']['car_id'])->select();
        $data['cars_park'] = CarsPark::all();
        return ResultVOUtil::success($data);
    }
    
    private function update($data){
        try{
            
            $model = new Cars;
            $model->where('car_id','in',$data['car_id'])->update(['car_park'=>$data['car_park']]);
        }catch (\Exception $e){         
            Log::record('更新cars表停车场:'.$e->getMessage(),'error');
            return false;
        }
        return true;
    }
    
    public function edit(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->dispatchService->edit($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::WRITE_ERROR);
            }
        }
    }
    
    public function dispatchLog(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->dispatchService->dispatchLog($param);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(ResultEnum::FIND_ERROR);
            }
        }
    }
    
    public function delete($id = 0){
        $data = $this->dispatchService->delete($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::DEL_ERROR);
        }
    }
    
    
}
